<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Orders extends ZhiyeAdmin_Controller {

    public function __construct(){

        parent::__construct();

		$this->load->model('orders_model','orders');
        $this->load->library('curl');

    }

	public function index($page_no=0){
		$search["order_id"]=$_GET['order_id'];
		$search["trade_id"]=$_GET['trade_id'];
		$search["account"]=$_GET['account'];
		$search["market"]=$_GET['market'];
		$search["side"]=$_GET['side'];
		$search["price"]=$_GET['price'];
		$where=array();
		if($search["order_id"]!=""){
			array_push($where,"order_id like '%".$search["order_id"]."%'");
        }
        if($search["trade_id"]!=""){
            array_push($where,"trade_id =".$search["trade_id"]);
		}
		if($search["account"]!=""){
			array_push($where,"account like '%".$search["account"]."%'");
		}
		if($search["market"]!=""){
			array_push($where,"market like '%".$search["market"]."%'");
		}
		if($search["side"]!=""){
			array_push($where,"side like '%".$search["side"]."%'");
		}
		if($search["price"]!=""){
			array_push($where,"order_id like '%".$search["price"]."%'");
        }
		if($search["submit"]==""){
			array_push($where,"orders.is_delete =0");
		}
		$page_no = max(intval($page_no),1);
		$this->orders->order('id DESC');
		if(count($where)>0)
			$this->orders->where($where);
        $data=$this->orders
						 ->select_all("orders.*",null,null,true,$page_no,"orders");
		$data['moudle_name']="订单管理";
		$data['table_name']="Orders";

		//p($data);die();
		$data['pages']=$this->orders->pages($data["total_num"]);
		$data['search']=$search;

        $this->zhiyeAdmin_view("lists", $data);		
        
    }

    public function edit($id=0){
		
		$data['moudle_name']="订单信息";
		$data['table_name']="Orders";

		if($id>0){
			$data["orders"]=$this->orders->select_one($id);
        }
        $this->zhiyeAdmin_view('edit',$data);
        
	}
	public function update($id=0){
		$orders=$this->input->post('orders');
		if($id>0){
			$this->orders->update($orders, array('id='.$id));
		}else{
			$this->orders->insert($orders);
		}
		redirect('admin/orders/index');
	}

	public function delete($checkIds=0){
		$checkIds=$_GET['checkIds'];
		//$str = 'apple,banana';
		$ids = explode(',', $checkIds);
		foreach ( $ids as $id )
		{
			if($id!=''){
				$this->orders->delete_one($id);
			}
			echo $id;
		}
		redirect('admin/orders/index');
	}
	public function orders_clear($checkIds=0){
		//获取系统值
        $this->load->model('system_setup_model','system_setup');
        $where=" where is_delete=0";
        $sql="SELECT * FROM `system_setup` ".$where;
        $data['system']=$this->system_setup->execute($sql,true,0);
        foreach (json_decode(json_encode($data['system'])) as $system )
        {
            $system_setup[$system->remarks.'_system']=$system->key_value;
        }
        $appid=$system_setup['appid_system'];
        $otp_code=$system_setup['otp_code_system'];
        
        // get account1 sessions   
        $account1_email="test1@sccx.exchange";
        $account1_password="Welcome111";
        $url = $system_setup['sessions_url_system']."sessions?email=$account1_email&password=$account1_password&application_id=$appid&otp_code=$otp_code";
        $account1_sessions=$this->curl->ssl()->simple_post($url);
        $account1_sessions=str_replace("\"","",$account1_sessions);
		echo $account1_sessions."<hr />";          

        // get account2 sessions
        $account2_email="test2@sccx.exchange";
        $account2_password="Welcome222";
        $url = $system_setup['sessions_url_system']."sessions?email=$account2_email&password=$account2_password&application_id=$appid&otp_code=$otp_code";
        $account2_sessions=$this->curl->ssl()->simple_post($url);
        $account2_sessions=str_replace("\"","",$account2_sessions);
		echo $account2_sessions."<hr />";          

        $headers1=array(
            "Authorization"=>" Bearer $account1_sessions",
        );
        $headers2=array(
            "Authorization"=>" Bearer $account2_sessions",
		);
		$url=$system_setup['api_url_system']."orders/clear";
		$clear_accounts1=$this->curl->ssl()->http_header($headers1)->simple_post($url);            
		echo $clear_accounts1."<hr />";          

		$clear_accounts2=$this->curl->ssl()->http_header($headers2)->simple_post($url);            
		echo $clear_accounts2."<hr />";          


	}
}